package com.google.appengine.a4ieve.entities;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PropertyProjection;
import org.json.JSONObject;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class StudentWrapper extends EntityWrapper{

    public static final String KIND_KEY = "Students";
    public static final String USER_ID_PROPERTY = "nickname";
    public static final String GROUP_KEY_PROPERTY = "group";
    public static final String STUDENT_NAME_PROPERTY = "fullname";
    public static final PropertyProjection ID_PROJECTION = new PropertyProjection(USER_ID_PROPERTY, String.class);
    public static final PropertyProjection GROUP_KEY_PROJECTION = new PropertyProjection(GROUP_KEY_PROPERTY, String.class);
    public static final PropertyProjection NAME_PROJECTION = new PropertyProjection(STUDENT_NAME_PROPERTY, String.class);

    private Entity entity;

    private static final Map<String, ConversionMethod> JSON_PROPERTIES;
    static {
        Map<String, ConversionMethod> temp = new HashMap<String, ConversionMethod>();
        temp.put(USER_ID_PROPERTY, null);
        temp.put(GROUP_KEY_PROPERTY, null);
        temp.put(STUDENT_NAME_PROPERTY, null);
        JSON_PROPERTIES = Collections.unmodifiableMap(temp);
    }

    public StudentWrapper(Entity e) {
        super(e);
    }

    @Override
    public JSONObject toJson() {
        return toJson(JSON_PROPERTIES);
    }

    @Override
    public Map<String, ConversionMethod> getProperties() {
        return JSON_PROPERTIES;
    }

    public static class StudentBuilder {
        private String googleId;
        private String groupId;
        private String name;
        private Entity entity;

        public StudentBuilder() {}

        public StudentBuilder googleId(String id) {
            this.googleId = id;
            Key googleKey = KeyFactory.createKey(KIND_KEY, id);
            entity = new Entity(KIND_KEY, googleKey);
            entity.setProperty(USER_ID_PROPERTY, id);
            return this;
        }

        public StudentBuilder groupId(String id) {
            if (this.googleId == null) {
                throw new IllegalStateException("Set google id prior to group id");
            }
            this.groupId = id;
            entity.setProperty(GROUP_KEY_PROPERTY, this.groupId);
            return this;
        }

        public StudentBuilder name(String name) {
            if (this.googleId == null) {
                throw new IllegalStateException("Set google id prior to group id");
            }
            this.name = name;
            entity.setProperty(STUDENT_NAME_PROPERTY, this.name);
            return this;
        }

        public StudentWrapper build() {
            return new StudentWrapper(this);
        }
    }

    private StudentWrapper(StudentBuilder builder) {
        this.entity = builder.entity;
    }

    public Entity getEntity(){
        return this.entity;
    }
}
